//
//  CXHToBidNativeExpressAd.m
//  CXHAdSDK-ToBid
//
//  Created by 陈坤 on 2025/4/9.
//

#import "CXHToBidNativeExpressAd.h"
#import <WindMillSDK/WindMillSDK.h>
#import <WindFoundation/WindFoundation.h>
#import <CXHAdSDK-Core/CXHAdSDKNativeExpressAd.h>
#import "CXHToBidNativeAdViewCreator.h"
#import <CXHAdSDK-Adapter/CXHAdapterNativeExpressCallBack.h>

@interface CXHToBidNativeExpressAd ()<CXHAdSDKNativeExpressAdDelegate>

@property (nonatomic, weak) id<AWMCustomNativeAdapterBridge> bridge;
@property (nonatomic, weak) id<AWMCustomNativeAdapter> adapter;
@property (nonatomic, strong) CXHAdSDKNativeExpressAd *nativeExpressAdManager;


@end

@implementation CXHToBidNativeExpressAd

- (instancetype)initWithBridge:(id<AWMCustomAdapterBridge>)bridge adapter:(id<AWMCustomAdapter>)adapter {
    self = [super init];
    if (self) {
        _bridge = (id<AWMCustomNativeAdapterBridge>)bridge;
        _adapter = (id<AWMCustomNativeAdapter>)adapter;
    }
    return self;
}
- (void)loadAdWithPlacementId:(NSString *)placementId adSize:(CGSize)size parameter:(AWMParameter *)parameter {
    //高度为0是自适应
    self.nativeExpressAdManager = [[CXHAdSDKNativeExpressAd alloc] initWithAdSize:size];
    self.nativeExpressAdManager.delegate = self;
    self.nativeExpressAdManager.posId  = placementId;
    if (parameter.isHeaderBidding) {
        [self.nativeExpressAdManager load:1];
    }else {
        int count = [[parameter.extra objectForKey:AWMAdLoadingParamNALoadAdCount] intValue];
        [self.nativeExpressAdManager load:count];
    }
}
- (void)didReceiveBidResult:(AWMMediaBidResult *)result {
    if (result.win) {
        [self.nativeExpressAdManager sendWinNotice:result.winnerPrice];
        return;
    }
    // todo: 当胜出时，winnerPrice有值，其它情况不回传胜出价
    [self.nativeExpressAdManager sendLossNotice:result.winnerPrice];
}

#pragma mark - CXHAdSDKNativeExpressAdDelegate

/**
 信息流广告请求成功

 @param nativeAd 广告实例
 @param adViewArray 广告模板集合
 */
- (void)CXH_nativeAdSucessToLoad:(CXHAdSDKNativeExpressAd *)nativeAd
                     adViewArray:(NSArray<__kindof UIView<CXHAdapterNativeExpressAdViewDelegate> *> *)adViewArray{
    WindmillLogDebug(@"[cxh]", @"%@", NSStringFromSelector(_cmd));
    NSString *price = [NSString stringWithFormat:@"%ld", [self.nativeExpressAdManager getBidPrice]];
    [self.bridge nativeAd:self.adapter didAdServerResponseWithExt:@{
        AWMMediaAdLoadingExtECPM: price
    }];
    NSMutableArray *adArray = [[NSMutableArray alloc] init];
    for (UIView<CXHAdapterNativeExpressAdViewDelegate> *adView in adViewArray) {
        AWMMediatedNativeAd *mNativeAd = [[AWMMediatedNativeAd alloc] init];
//        mNativeAd.originMediatedNativeAd = adView;
        mNativeAd.viewCreator = [[CXHToBidNativeAdViewCreator alloc] initWithExpressAdView:adView];
        mNativeAd.view = adView;
        mNativeAd.originMediatedNativeAd = adView;
//        [adView render];
        [adArray addObject:mNativeAd];
    }
    [self.bridge nativeAd:self.adapter didLoadWithNativeAds:adArray];
}

/**
 信息流广告请求失败

 @param nativeAd 广告实例
 @param error 请求错误描述
 */
- (void)CXH_nativeAdFailToLoad:(CXHAdSDKNativeExpressAd *)nativeAd
                         error:(NSError *)error{
    WindmillLogDebug(@"[cxh]", @"%@", NSStringFromSelector(_cmd));
//    self.adViews = nil;
    [self.bridge nativeAd:self.adapter didLoadFailWithError:error];
}

/**
 信息流广告渲染成功

 @param adView 广告视图
 */
- (void)CXH_nativeAdViewRenderOrRegistSuccess:(UIView<CXHAdapterNativeExpressAdViewDelegate> *)adView{
    WindmillLogDebug(@"[cxh]", @"%@", NSStringFromSelector(_cmd));
    [self.bridge nativeAd:self.adapter renderSuccessWithExpressView:adView];
}

/**
 信息流广告渲染失败

 @param adView 广告视图
 */
- (void)CXH_nativeAdViewRenderOrRegistFail:(UIView<CXHAdapterNativeExpressAdViewDelegate> *)adView{
    NSError *err = [NSError errorWithDomain:@"com.cxh.nativeExpress"
                                       code:100
                                   userInfo:@{}];
    WindmillLogDebug(@"[cxh]", @"%@", NSStringFromSelector(_cmd));
    [self.bridge nativeAd:self.adapter renderFailWithExpressView:adView andError:err];
}

/**
 信息流广告被点击

 @param nativeAd 广告模板
 */
- (void)CXH_nativeAdClicked:(CXHAdSDKNativeExpressAd *)nativeAd adView:(__kindof UIView<CXHAdapterNativeExpressAdViewDelegate> *)adView loadingPageURL:(NSString *)loadingPageURL{
    WindmillLogDebug(@"[cxh]", @"%@", NSStringFromSelector(_cmd));
    [self.bridge nativeAd:self.adapter didClickWithMediatedNativeAd:adView];
}

/**
 信息流广告被关闭
 
 @param nativeAd 广告模板
 */
- (void)CXH_nativeAdClose:(CXHAdSDKNativeExpressAd *)nativeAd adView:(__kindof UIView<CXHAdapterNativeExpressAdViewDelegate> *)adView{
    WindmillLogDebug(@"[cxh]", @"%@", NSStringFromSelector(_cmd));
    [self.bridge nativeAd:self.adapter didClose:adView closeReasons:nil];
}

/**
 信息流广告被展示
 
 @param nativeAd 广告实例
 */
- (void)CXH_nativeAdExposure:(CXHAdSDKNativeExpressAd *)nativeAd adView:(__kindof UIView<CXHAdapterNativeExpressAdViewDelegate> *)adView{
    WindmillLogDebug(@"CSJ", @"%@", NSStringFromSelector(_cmd));
    [self.bridge nativeAd:self.adapter didVisibleWithMediatedNativeAd:adView];
}

/**
 信息流广告展示上报
 
 @param nativeAd 广告实例
 */
- (void)CXH_nativeAdExposureReport:(CXHAdSDKNativeExpressAd *)nativeAd
                            adView:(__kindof UIView<CXHAdapterNativeExpressAdViewDelegate> *)adView{
    
}

/**
 信息流广告点击上报

 @param nativeAd 广告模板
 */
- (void)CXH_nativeAdClickedReport:(CXHAdSDKNativeExpressAd *)nativeAd
                           adView:(__kindof UIView<CXHAdapterNativeExpressAdViewDelegate> *)adView{
    
}

/**
 信息流广告落地页被关闭
 
 @param nativeAd 广告实例
 */
- (void)CXH_nativeAdCloseLandingPage:(CXHAdSDKNativeExpressAd *)nativeAd
                              adView:(__kindof UIView<CXHAdapterNativeExpressAdViewDelegate> *)adView{
    WindmillLogDebug(@"[cxh]", @"%@", NSStringFromSelector(_cmd));
    [self.bridge nativeAd:self.adapter didDismissFullScreenModalWithMediatedNativeAd:adView];
}

@end
